//
//  SecurityMessageCell.m
//  LocomotiveGuards
//
//  Created by xys on 15/11/21.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "SecurityMessageCell.h"
#import "Masonry.h"
#import "SecurityMessage.h"
#import "SecurityHearder.h"

#define MESSAGE_DATELABEL_TEXT_COLOR    RGBCOLOR(214,210,214)
#define MESSAGE_DATELABEL_TEXT_FRONT    [UIFont systemFontOfSize:12]

#define MESSAGE_MESSAGELABEL_BACK_TEXT_COLOR    RGBCOLOR(237,237,237)


@interface SecurityMessageCell ()
{

}
@property (nonatomic,assign) BOOL checkBox;
@property (nonatomic,strong) UILabel     * dateLabel;
@property (nonatomic,strong) UIView      * backView;
@property (nonatomic,strong) UIImageView * checkBoxImageView;
@property (nonatomic,strong) UIImageView * msgTypeImageView;
@property (nonatomic,strong) UILabel     * messageLabel;

@end

@implementation SecurityMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier hasCheckBox:(BOOL) checkBox
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.checkBox = checkBox;
        [self initView];
    }
    return self;
}


- (void)initView
{
    self.dateLabel = [[UILabel alloc]init];
    self.backgroundColor = [UIColor whiteColor];
    self.dateLabel.textColor = MESSAGE_DATELABEL_TEXT_COLOR;
    self.dateLabel.font = MESSAGE_DATELABEL_TEXT_FRONT;
    self.dateLabel.textAlignment = NSTextAlignmentCenter;
    
    self.backView = [[UIView alloc]init];
    self.backView.backgroundColor = MESSAGE_MESSAGELABEL_BACK_TEXT_COLOR;
    
    self.messageLabel = [[UILabel alloc]init];
    self.messageLabel.textColor = [UIColor blackColor];
    self.messageLabel.font = MESSAGE_MESSAGELABEL_TEXT_FRONT;
    self.messageLabel.numberOfLines = 0;
    
    self.msgTypeImageView = [[UIImageView alloc]init];
    
    [self.contentView addSubview:self.dateLabel];
    [self.contentView addSubview:self.backView];
    [self.contentView addSubview:self.messageLabel];
    [self.contentView addSubview:self.msgTypeImageView];
    
    if (self.checkBox)
    {
        self.checkBoxImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:self.checkBoxImageView];
    }
    
    /*
        布局
     */
    [self.dateLabel makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.contentView.top);
         make.left.equalTo(self.contentView.left);
         make.right.equalTo(self.contentView.right);
         make.height.equalTo(MESSAGE_DATELABEL_HEIGHT);
     }];
    
    [self.messageLabel makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.dateLabel.bottom);
         if (self.checkBox)
         {
             make.left.equalTo(self.contentView.left).with.offset(3.8 *MESSAGE_MESSAGEICON_SIZE);
         }
         else
         {
              make.left.equalTo(self.contentView.left).with.offset(2*MESSAGE_MESSAGEICON_SIZE);
         }
         make.right.equalTo(self.contentView.right).with.offset(-MESSAGE_MESSAGEICON_SIZE);
         make.bottom.equalTo(self.contentView.bottom);
     }];
    
    if (self.checkBox)
    {
        [self.checkBoxImageView makeConstraints:^(MASConstraintMaker *make)
         {
             make.centerY.equalTo(self.messageLabel.centerY);
             make.left.equalTo(self.contentView.left).with.offset(MESSAGE_MESSAGEICON_SIZE*0.6);
             make.height.equalTo(MESSAGE_MESSAGEICON_SIZE);
             make.width.equalTo(MESSAGE_MESSAGEICON_SIZE);
         }];
        
        [self.msgTypeImageView makeConstraints:^(MASConstraintMaker *make)
         {
             make.centerY.equalTo(self.messageLabel.centerY);
             make.left.equalTo(self.checkBoxImageView.right).with.offset(MESSAGE_MESSAGEICON_SIZE*0.6);
             make.height.equalTo(MESSAGE_MESSAGEICON_SIZE);
             make.width.equalTo(MESSAGE_MESSAGEICON_SIZE);
         }];
    }
    else
    {
        [self.msgTypeImageView makeConstraints:^(MASConstraintMaker *make)
         {
             make.centerY.equalTo(self.messageLabel.centerY);
             make.left.equalTo(self.contentView.left).with.offset(MESSAGE_MESSAGEICON_SIZE*0.6);
             make.height.equalTo(MESSAGE_MESSAGEICON_SIZE);
             make.width.equalTo(MESSAGE_MESSAGEICON_SIZE);
         }];
        
    }
    [self.backView makeConstraints:^(MASConstraintMaker *make)
    {
        make.centerY.equalTo(self.messageLabel.centerY);
        make.height.equalTo(self.messageLabel.height);
        make.left.equalTo(self.contentView);
        make.width.equalTo(self.contentView);
    }];
}

- (void)setSecurityMessage:(SecurityMessage *)securityMessage
{
    if (!securityMessage)
    {
        return;
    }
    
    self.dateLabel.text = securityMessage.dateString;
    if (self.checkBox)
    {
         self.checkBoxImageView.image = [UIImage imageNamed:@"security_7pic_choice"];
    }
    
    self.messageLabel.text = securityMessage.msgString;
    
    /*
        设置消息故障类型图标
     */
    switch (securityMessage.msgType)
    {
        //异常状态
        case AlarmMessageTypeAbnormal:
            self.msgTypeImageView.image = [UIImage imageNamed:@"security_7pic_icon02"];
            break;
           
        //低电报警
        case AlarmMessageTypeLowPower:
            self.msgTypeImageView.image = [UIImage imageNamed:@"security_7pic_icon01"];
            break;
        
        //剪断报警
        case AlarmMessageTypeCut:
            self.msgTypeImageView.image = [UIImage imageNamed:@"security_7pic_icon03"];
            break;
        
        //电子围栏
        case AlarmMessageTypeElecFence:
            self.msgTypeImageView.image = [UIImage imageNamed:@"security_7pic_icon04"];
            break;
         
        //非法位移
        case AlarmMessageTypeIllegalDisplacement:
            self.msgTypeImageView.image = [UIImage imageNamed:@"security_7pic_icon05"];
            break;
           
        //震动
        case AlarmMessageTypeIllegalVibration:
            self.msgTypeImageView.image = [UIImage imageNamed:@"security_7pic_icon06"];
            break;
            
        //锁定
        case AlarmMessageTypeIllegalLock:
            self.msgTypeImageView.image = [UIImage imageNamed:@"security_7pic_icon07"];
            break;
            
        default:
            break;
    }
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    if (!self.checkBox)
    {
        return;
    }
    
    if ( selected )
    {
        self.checkBoxImageView.image = [UIImage imageNamed:@"security_7pic_choice_on"];
    }
    else
    {
        self.checkBoxImageView.image = [UIImage imageNamed:@"security_7pic_choice"];
    }
}

@end
